package bean;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import java.util.Vector;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class DataLoad1 {
	
	public static TreeMap getNodeTypeBeans(String typeFile) throws Exception {
		return loadNodeTypeBean(typeFile);
	}
	
	public static ArrayList getNodeBeans(String nodeFile) throws Exception {
		return loadNodeBeans(nodeFile);
	}
	
	
	private static TreeMap loadNodeTypeBean(String xmlFile) throws Exception {
		TreeMap nodeTypeBeans = new TreeMap ();
        SAXReader reader = new SAXReader();
        File file = new File(xmlFile);
        Document document = reader.read(file);
        Element root = document.getRootElement();
        List<Element> childElements = root.elements();
        for (Element child : childElements) {
        	NodeTypeBean node = new NodeTypeBean();
//        	System.out.println("type: " + child.attributeValue("type"));
            Element telnetElement = child.element("telnet");
            node.termtype = telnetElement.attributeValue("termtype");
            node.loginPrompt = telnetElement.attributeValue("loginPrompt");
            node.passwordPrompt = telnetElement.attributeValue("passwordPrompt");
            node.commandPrompt = telnetElement.attributeValue("commandPrompt");
            
            Element parserElement = child.element("parser");
            node.parser = parserElement.attributeValue("class");
//            System.out.println("termtype: " + telnetElement.attributeValue("termtype"));
//            System.out.println("loginPrompt: " + telnetElement.attributeValue("loginPrompt"));
            
            List<Element> commandElements = child.element("commands").elements();            
            for (Element commandElement : commandElements) {
            	//System.out.println("command: " + commandElement.getStringValue());
            	node.commands.add(commandElement.getStringValue());
            	String command = commandElement.getStringValue();
            }
            nodeTypeBeans.put(child.attributeValue("type"), node);            
        }        
        return nodeTypeBeans;

    }
	
	private static ArrayList loadNodeBeans(String nodeFile) {
		ArrayList nodes = new ArrayList();
//		nodes.add(new NodeBean("Win", "115.28.223.22", 23, "administrator",
//				"xinwin401"));
//		nodes.add(new NodeBean("Linux", "192.168.0.224", 23, "wulian", "wulian"));
//		nodes.add(new NodeBean("Raisecom", "10.2.254.254", 23, "raisecom", "raisecom"));
//		nodes.add(new NodeBean("Raisecom", "10.2.254.250", 23, "raisecom", "raisecom"));
		File file=new File(nodeFile);
		try {
			if(!file.exists()||file.isDirectory())
			    throw new FileNotFoundException();
			BufferedReader br=new BufferedReader(new FileReader(file));
			String temp=br.readLine();
			temp=br.readLine();//忽略首行
			String[] node;			
			while(temp!=null){
				//System.out.println(temp);
				if (temp.startsWith("#")) {
					temp=br.readLine();
					continue;
				}
			    node = temp.split("\\s+"); //以n个空格切分
			   	nodes.add(new NodeBean(node[0], node[1], Integer.valueOf(node[2]).intValue(), node[3], node[4]));
			    temp=br.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return nodes;
	}
	
	public static void main(String[] args) throws Exception {
		
		String rootPath = args.length >0 ? args[0]:"E:/workspace/javaTelenet/bin/";
		String typeFile = rootPath + "telnet-nodetypes.xml";
		String nodeFile = rootPath + "telnet-nodes.txt";
		
		TreeMap nodeTypeBeans = DataLoad.getNodeTypeBeans(typeFile);
		Iterator iterator = nodeTypeBeans.keySet().iterator();    
		while (iterator.hasNext()) {    
		   Object key = iterator.next();    
		   System.out.println(key +":\n"+ ((NodeTypeBean)(nodeTypeBeans.get(key))).toStr()+"\n");    
		}
		
		/*ArrayList nodes = DataLoad.getNodeBeans(nodeFile);
		int size = nodes.size();
		for (int i = 0; i < size; i++) { // 对每个节点
			NodeBean node = (NodeBean) nodes.get(i);
			System.out.println(node.toStr());
		}*/
	}

}
